package com.project.hqshorttaxi.controller;

import com.github.pagehelper.PageHelper;
import com.project.hqshorttaxi.aop.TagLog;
import com.project.hqshorttaxi.dto.WhiteListParam;
import com.project.hqshorttaxi.entity.Login;
import com.project.hqshorttaxi.entity.WhiteList;
import com.project.hqshorttaxi.service.LoginService;
import com.project.hqshorttaxi.service.WhiteListService;
import com.project.hqshorttaxi.util.PageInfo;
import com.project.hqshorttaxi.util.RestResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/api/whitelist/")
public class WhiteListController {

    @Resource
    WhiteListService whiteListService;
    @Resource
    LoginService loginService;

    /**
     * 新增
     */
    @PostMapping("add")
    @TagLog(operation = 2, content = "新增白名单")
    public RestResult add(@RequestHeader(value = "authorization") String token, @RequestBody WhiteList param) {
        try {
            Login entity = loginService.getByToken(token);
            if (entity == null) {
                return RestResult.error("用户不存在");
            }
            param.setCreateUser(entity.getUserName());
            param.setUpdateUser(entity.getUserName());
            whiteListService.insert(param);
            return RestResult.ok();
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 更新
     */
    @PostMapping("update")
    @TagLog(operation = 3, content = "更新白名单")
    public RestResult update(@RequestHeader(value = "authorization") String token, @RequestBody WhiteList param) {
        try {
            WhiteList entity = whiteListService.getById(param.getId());
            if (entity == null) {
                return RestResult.error("该条数据不存在");
            } else {
                Login e = loginService.getByToken(token);
                if (e == null) {
                    return RestResult.error("用户不存在");
                }
                param.setUpdateUser(e.getUserName());
                param.setUpdateTime(new Date());
                whiteListService.update(param);
                return RestResult.ok();
            }
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @PostMapping("delete/{id}")
    @TagLog(operation = 4, content = "删除白名单")
    public RestResult delete(@PathVariable Integer id) {
        try {
            WhiteList entity = whiteListService.getById(id);
            if (entity == null) {
                return RestResult.error("该条数据不存在");
            } else {
                whiteListService.delete(id);
                return RestResult.ok();
            }
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 列表
     */
    @PostMapping("page")
    @TagLog(operation = 0, content = "查询白名单列表")
    public RestResult page(@RequestBody WhiteListParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        return RestResult.ok(new PageInfo<>(whiteListService.list(param)));
    }

    /**
     * 根据ID获取
     */
    @GetMapping("{id}")
    @TagLog(operation = 0, content = "根据ID获取白名单详细信息")
    public RestResult pageById(@PathVariable Integer id) {
        return RestResult.ok(whiteListService.getById(id));
    }

}
